package com.rf.richfitwheel.admin.utils;

import com.rf.richfitwheel.admin.sys.vo.UserVO;
import com.rf.richfitwheel.common.utils.TreeUtils;
import com.rf.richfitwheel.common.utils.UuidUtil;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import java.security.SecureRandom;
import java.util.*;



public class PasswordHelper {
	private static RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
	private static String algorithmName = "md5";
	private static int hashIterations = 2;
	
	private static final String JASYPT_ENCRYPTOR = "EUCmheFQkjjZP4yLfxKRlZLaHTjjkkxi9dSbxT68";

	public static UserVO encryptPassword(UserVO userVO) {
		String salt = "";
		if(userVO.getSalt() != null && !userVO.getSalt().equals("")){
			salt = userVO.getSalt();
		}else{
			salt = randomNumberGenerator.nextBytes().toHex();
		}
		userVO.setSalt(salt);
		String newPassword = new SimpleHash(algorithmName, userVO.getPassword(), ByteSource.Util.bytes(userVO.getUsername()+salt), hashIterations).toHex();
		userVO.setPassword(newPassword);
		return userVO;
	}
	
	/**
     * 随机生成密码
     * @param length 密码的长度
     * @return 最终生成的密码
     */
	public static String generatePassword (int length) {
        // 最终生成的密码
        String password = "";
		SecureRandom random = new SecureRandom();
        for (int i = 0; i < length; i ++) {
            // 随机生成0或1，用来确定是当前使用数字还是字母 (0则输出数字，1则输出字母)
            int charOrNum = random.nextInt(2);
            if (charOrNum == 1) {
                // 随机生成0或1，用来判断是大写字母还是小写字母 (0则输出小写字母，1则输出大写字母)
                int temp = random.nextInt(2) == 1 ? 65 : 97;
                password += (char) (random.nextInt(26) + temp);
            } else {
                // 生成随机数字
                password += random.nextInt(10);
            }
        }
        return password;
    }

    List<TreeUtils> getStationList() throws Exception {
		List<Map<String, Object>> list = new ArrayList<>();
		String[][] ss = {{"0","930ae3531b944104a012b6cbf7bcde6e","11","中国石油集团养老资产管理有限责任公司","0","true","el-icon-folder-opened","1.000000000000000000000000000000"},
		{"0","5f7d8386c2714fba90693205a5ec6df4","1101","公司领导","11","true","el-icon-folder-opened","1.000000000000000000000000000000"},
		{"0","492bef804a9d4099a65b4f975af9f83a","1107","资产配置部","11","true","el-icon-folder-opened","2.000000000000000000000000000000"},
		{"0","58c31f50b72849478a766ff559d66c9d","1104","委托投资部","11","true","el-icon-folder-opened","3.000000000000000000000000000000"},
		{"0","5a602f6de19b41398b5946e090bd1a53","1109","风控合规部","11","true","el-icon-folder-opened","4.000000000000000000000000000000"},
		{"0","e0727b0bc88849eab29f867bf1b63f18","1108","账户管理部","11","true","el-icon-folder-opened","5.000000000000000000000000000000"},
		{"0","c7ac093c85af48c6ba461042cd0f9c3b","1106","综合管理部","11","true","el-icon-folder-opened","6.000000000000000000000000000000"},
		{"1","aa1bc2c2b4614cca958445fced547d54","S34","理事会办公室主任","1101","false","el-icon-user","1"},
		{"1","6a3911498a41481199e7304f0deca302","S52","党支部书记","1101","false","el-icon-user","2"},
		{"1","5803f83075ad4869bb4905a7192aab10","S40","理事会办公室副主任","1101","false","el-icon-user","2"},
		{"1","2f0a46804bc24ce8adcf0fcdf71a6eda","S49","何少锋","1101","false","el-icon-user","3"},
		{"1","63e02924f1084c58a6279fb871d9dd7c","S38","分管领导","1109","false","el-icon-user","3"},
		{"1","b3b61b2be7b948bea65089b8d444b7c0","S39","分管领导","1108","false","el-icon-user","3"},
		{"1","f2e332e5bdde4e7d9b92538a1f383de6","S51","王洪军","1101","false","el-icon-user","3"},
		{"1","5a6f856c34a9489cb2f5da5da49d2d97","S37","分管领导","1104","false","el-icon-user","3"},
		{"1","e046ad37761a4dabbcf6fe4d462ee3da","S36","分管领导","1107","false","el-icon-user","3"},
		{"1","f16b25ec67a048dabc5644c7cc83249c","S35","分管副主任","1106","false","el-icon-user","3"},
		{"1","f10b97dd091d458e93e4c39bd404b964","S16","经理","1104","false","el-icon-user","4"},
		{"1","daca9ceb88624976af3b88d805e3d447","S26","部门经理","1106","false","el-icon-user","4"},
		{"1","a62dcbbfe3f943ba8c721e04181aecbe","S22","经理","1108","false","el-icon-user","4"},
		{"1","9e89d779beca461482f4c44c4d5cbc86","S20","经理","1109","false","el-icon-user","4"},
		{"1","0c3f1d2d55ce4d09aa518b379aef8875","S11","经理","1107","false","el-icon-user","4"},
		{"1","648e519e798d44a1a91b3b3d1fcfb6d1","S17","副经理","1104","false","el-icon-user","5"},
		{"1","628cb49d58724e00864a886d0ee2b4bc","S12","副经理","1107","false","el-icon-user","5"},
		{"1","e5cd8c541fb4469caaee0566224e5995","S32","部门副经理","1106","false","el-icon-user","5"},
		{"1","063397d7eb004efd83ea9f6060dfd71a","S43","考勤员","1104","false","el-icon-user","6"},
		{"1","9489173561c94fd7973150592f5cd3ab","S41","考勤员","11","false","el-icon-user","6"},
		{"1","a8f38f89bd0f4f1984720dc6f941aac8","S53","李灵杰","1104","false","el-icon-user","6"},
		{"1","0112ec126500430aa25e1bf621b69121","S14","宏观研究岗","1107","false","el-icon-user","6"},
		{"1","2215a17aa51b47aebf403e8e43c5c6fd","S23","账户管理岗","1108","false","el-icon-user","6"},
		{"1","ed07c8d1dfe74e1ba537569f7b7d8752","S42","考勤员","1107","false","el-icon-user","6"},
		{"1","3887b7c76a2f4aa09521c751118e2d85","S45","考勤员","1108","false","el-icon-user","6"},
		{"1","b8b2cd280e71427b8573d5240d4f0867","S24","支付管理岗","1108","false","el-icon-user","6"},
		{"1","1347837f02c84af3abfa865315d77ab5","S7","通用员工岗","11","false","el-icon-user","6"},
		{"1","833c81f933a3413b862079992ac8128c","S18","投资监督岗","1104","false","el-icon-user","6"},
		{"1","ec6b7b266754499eb44d3aeeba79e397","S19","财务会计岗","1104","false","el-icon-user","6"},
		{"1","38518ff7ea5d42fdbfb2202c77a03250","S28","人力资源岗","1106","false","el-icon-user","6"},
		{"1","a364124400204ecda99b61d4b1735598","S46","考勤员","1106","false","el-icon-user","6"},
		{"1","f9a93f383e564153899b3244a8e171e7","S44","考勤员","1109","false","el-icon-user","6"},
		{"1","5ca45d32840c490a8d5d45f816d40dc1","S13","信息管理岗","1107","false","el-icon-user","6"},
		{"1","0c59c87e08824679891f4451ed82215e","S25","资金出纳岗","1104","false","el-icon-user","6"},
		{"1","70b8662eb1f04c7c85433b4f56bfa364","S15","养老金产品管理岗","1107","false","el-icon-user","6"},
		{"1","37a6be7cd9e74280a3219514c06804f4","S27","综合管理岗","1106","false","el-icon-user","6"},
		{"1","9cc9fe3fa2224f56af47e87e0dbed89e","S21","风控合规岗","1109","false","el-icon-user","6"},
		{"1","dc7c172bb0ac4ed1bda9ded0ef1c301b","S52","审计01","11","false","el-icon-user","9"},
		{"1","40c85f03d29746db89565f6c7811690e","S50","魏瑞琦","1104","false","el-icon-user","9"},
		{"1","877e520a93eb46a1ac6c1a06683ff84d","S47","苗艳","1104","false","el-icon-user","9"},
		{"1","af8fc6315ac8462f901cb665b72aa98f","S48","吴桐","1107","false","el-icon-user","9"},
		{"1","379fe336ad374b9faf98ba14a038bb28","S29","系统管理员","11","false","el-icon-user","9"}};
		for (int i = 0; i < ss.length; i++){
			Map<String, Object> map = getMap(ss[i][0], ss[i][1], ss[i][2], ss[i][3], ss[i][4], ss[i][5], ss[i][6], ss[i][7]);
			list.add(map);
		}
		List<TreeUtils> listTree = TreeUtils.getTreeListMap(list, "org_id", "parent_org_id", "org_name", "disabledfn", "iconfn");
		return listTree;
	}

	Map<String, Object> getMap(String sortType, String id, String orgId, String orgName, String parentOrgId, String disabledfn, String iconfn, String ordernum){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sort_type", sortType);
		map.put("id", id);
		map.put("org_id", orgId);
		map.put("org_name", orgName);
		map.put("parent_org_id", parentOrgId);
		map.put("disabledfn", disabledfn);
		map.put("iconfn", iconfn);
		map.put("ordernum", ordernum);
		return map;
	}

	public static void main(String[] args) {

		/*PasswordHelper passwordHelper = new PasswordHelper();
		try {
			List<TreeUtils> a = passwordHelper.getStationList();
			System.out.println(a.toArray());
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		/*User user = new User();
		//user.setDescription("管理员");
		user.setUsername("admin");
		user.setPassword("111111");
		user.setSalt("4157c3feef4a6ed91b2c28cf4392f2d1");
		user = passwordHelper.encryptPassword(user);
		System.out.println("==password=="+user.getPassword()+"==========="+user.getUsername());*/
//		System.out.println(generatePassword(10));
		inithkUserPassword();
	}

	/**
	 * @功能描述: 密码生成工具类
	 * @auther: jiaozhongcai
	 * @date: 2021/6/2 14:04
	 * @param
	 * @return: void
	 */
	public void getPassword(){
		/**
		 * 密码生成过程
		 */
		String username= "80715422";
		String password  = "5lwFGDlA+tER33YFr6+oCA==";
		String salt= UuidUtil.get32UUID();
		System.out.println("盐值= "+salt);
		System.out.println(salt.length());
		salt=username+salt;
		int hashIterations = 2;
		SimpleHash simpleHash = new SimpleHash("md5",JasyptUtil.decrypt(JASYPT_ENCRYPTOR, password), ByteSource.Util.bytes(salt),hashIterations);
		System.out.println("密码结果= "+simpleHash.toHex());
	}
	
	/**
	 * 生产密码更新的脚本
	 */
	public static void inithkUserPassword(){
		String[] accounts = {"10101",
				"10201",
				"10301",
				"10401",
				"10501",
				"10601",
				"10701",
				"10801",
				"10901",
				"11001",
				"11101",
				"11201",
				"11301",
				"11401",
				"11501",
				"11601",
				"11701",
				"11801",
				"11901",
				"12001",
				"12101",
				"12201",
				"12301",
				"12401",
				"12501",
				"12601",
				"12701",
				"12801",
				"12901",
				"13001",
				"13101",
				"13201",
				"13301",
				"13401",
				"13501",
				"13601",
				"13701",
				"13801",
				"13901",
				"14001",
				"14101",
				"14201",
				"14301",
				"14401",
				"14501",
				"14601",
				"14701",
				"14801",
				"14901",
				"15001",
				"15101",
				"15201",
				"15301",
				"15401",
				"15501",
				"15601",
				"15701",
				"15801",
				"15901",
				"16001",
				"16101",
				"16201",
				"16301",
				"16401",
				"16501",
				"16601",
				"16701",
				"16801",
				"16901",
				"17001",
				"17101",
				"17201",
				"17301",
				"17401",
				"17501",
				"17601",
				"17701",
				"17801",
				"17901",
				"18001",
				"18101",
				"18201",
				"18301",
				"18401",
				"18501",
				"18601",
				"18701",
				"18801",
				"18901",
				"19001",
				"19101",
				"19201",
				"19301",
				"19401",
				"19501",
				"19601",
				"19701",
				"19801",
				"19901",
				"20001",
				"20101",
				"20201",
				"20301",
				"20401",
				"20501",
				"20601",
				"20701",
				"20801",
				"20901",
				"21001",
				"21101",
				"21201",
				"21301",
				"21401",
				"21501",
				"21601",
				"21701",
				"21801",
				"21901",
				"22001",
				"22101",
				"22201",
				"22301",
				"22401",
				"22501",
				"22601",
				"22701",
				"22801",
				"22901",
				"23001",
				"23101",
				"23201",
				"23301",
				"23401",
				"23501",
				"23601",
				"23701",
				"23801",
				"23901",
				"24001",
				"24101",
				"24201",
				"24301",
				"24401",
				"24501",
				"24601",
				"24701",
				"24801",
				"24901",
				"25001",
				"25101",
				"10102",
				"10202",
				"10302",
				"10402",
				"10502",
				"10602",
				"10702",
				"10802",
				"10902",
				"11002",
				"11102",
				"11202",
				"11302",
				"11402",
				"11502",
				"11602",
				"11702",
				"11802",
				"11902",
				"12002",
				"12102",
				"12202",
				"12302",
				"12402",
				"12502",
				"12602",
				"12702",
				"12802",
				"12902",
				"13002",
				"13102",
				"13202",
				"13302",
				"13402",
				"13502",
				"13602",
				"13702",
				"13802",
				"13902",
				"14002",
				"14102",
				"14202",
				"14302",
				"14402",
				"14502",
				"14602",
				"14702",
				"14802",
				"14902",
				"15002",
				"15102",
				"15202",
				"15302",
				"15402",
				"15502",
				"15602",
				"15702",
				"15802",
				"15902",
				"16002",
				"16102",
				"16202",
				"16302",
				"16402",
				"16502",
				"16602",
				"16702",
				"16802",
				"16902",
				"17002",
				"17102",
				"17202",
				"17302",
				"17402",
				"17502",
				"17602",
				"17702",
				"17802",
				"17902",
				"18002",
				"18102",
				"18202",
				"18302",
				"18402",
				"18502",
				"18602",
				"18702",
				"18802",
				"18902",
				"19002",
				"19102",
				"19202",
				"19302",
				"19402",
				"19502",
				"19602",
				"19702",
				"19802",
				"19902",
				"20002",
				"20102",
				"20202",
				"20302",
				"20402",
				"20502",
				"20602",
				"20702",
				"20802",
				"20902",
				"21002",
				"21102",
				"21202",
				"21302",
				"21402",
				"21502",
				"21602",
				"21702",
				"21802",
				"21902",
				"22002",
				"22102",
				"22202",
				"22302",
				"22402",
				"22502",
				"22602",
				"22702",
				"22802",
				"22902",
				"23002",
				"23102",
				"23202",
				"23302",
				"23402",
				"23502",
				"23602",
				"23702",
				"23802",
				"23902",
				"24002",
				"24102",
				"24202",
				"24302",
				"24402",
				"24502",
				"24602",
				"24702",
				"24802",
				"24902",
				"25002",
				"25102"};
		String password  = "gGG9DYo6XDuMq2H2GX63SHExXWekg+py";
		for(String account : accounts){
			String salt1= UuidUtil.get32UUID();
			String salt=account+salt1;
			int hashIterations = 2;
			SimpleHash simpleHash = new SimpleHash("md5",JasyptUtil.decrypt(JASYPT_ENCRYPTOR, password), ByteSource.Util.bytes(salt),hashIterations);
			System.out.println("update sys_user set password='" +simpleHash.toHex() + "',salt='" + salt1 + "' where username='" + account + "';");
		}
	}
}
